package com.ae.shield.common.gui.container;

import com.ae.shield.api.IShield;
import com.ae.shield.common.items.ItemIntegratedShield;
import com.ae.shield.common.items.shieldFittings.makers.ShieldMakerBase;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.container.Container;
import net.minecraft.inventory.container.Slot;
import net.minecraft.item.ItemStack;
import net.minecraft.network.PacketBuffer;
import net.minecraft.util.Hand;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.energy.IEnergyStorage;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.ItemStackHandler;
import net.minecraftforge.items.SlotItemHandler;

import javax.annotation.Nonnull;

public class ShieldContainer extends Container {
    private final ItemStack shield;
    private final IItemHandler cap;

    public ShieldContainer(int id, PlayerInventory playerInventory, ItemStack shieldIn) {
        super(ContainerList.SHIELD_CONTAINER.get(), id);
        shield = shieldIn;
        LazyOptional<IItemHandler> lazyOptional = shieldIn.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY);
        cap = lazyOptional.orElseThrow(RuntimeException::new);
        loadSlot(playerInventory);
    }

    public ShieldContainer(int id, PlayerInventory inventory, PacketBuffer buffer) {
        this(id, inventory, buffer.readItemStack());
    }

    private void loadSlot(PlayerInventory inventory) {
        IItemHandler cap = this.cap;

        if (inventory.player.world.isRemote) {
            cap = new ItemStackHandler(((ItemIntegratedShield) shield.getItem()).getSlots());
        }

        //创建玩家背包
        for (int row = 0; row < 3; row++) {
            for (int column = 0; column < 9; column++) {
                addSlot(new Slot(inventory, column + row * 9 + 9, 8 + column * 18, 174 - (4 - row) * 18 - 10));
            }
        }
        //创建工具栏
        for (int col = 0; col < 9; ++col) {
            addSlot(new Slot(inventory, col, 8 + col * 18, 150));
        }
        //创建护盾格子
        for (int i = 0; i < 3; i++) {
            addSlot(new ShieldSlot(cap, i, 33 + 47 * i, 17));
        }

        for (int i = 3; i < 6; i++) {
            addSlot(new ShieldSlot(cap, i, 127 - 47 * (i - 3), 39));
        }

        for (int i = 6; i < 9; i++) {
            addSlot(new ShieldSlot(cap, i, 33 + 47 * (i - 6), 61));
        }
    }

    private static class ShieldSlot extends SlotItemHandler {
        public ShieldSlot(IItemHandler itemHandler, int index, int xPosition, int yPosition) {
            super(itemHandler, index, xPosition, yPosition);
        }

        public boolean isItemValid(@Nonnull ItemStack stack) {
            return stack.getItem() instanceof IShield;
        }
    }

    //在gui内shift点击物品时的逻辑
    @Nonnull
    @Override
    public ItemStack transferStackInSlot(@Nonnull PlayerEntity playerIn, int slotIn) {
        ItemStack stack;
        Slot slot = inventorySlots.get(slotIn);

        if (slot == null || !slot.getHasStack()) {
            return ItemStack.EMPTY;
        }

        ItemStack itemStack = slot.getStack();
        stack = itemStack.copy();
        int invStart = 0;
        int invEnd = invStart + 27;
        int shortEnd = invEnd + 9;
        int guiEnd = shortEnd + cap.getSlots();

        if (slotIn < shortEnd) {
            if (!(itemStack.getItem() instanceof ShieldMakerBase) || !mergeItemStack(itemStack, shortEnd, guiEnd, false)) {
                return ItemStack.EMPTY;
            }
        } else {
            if (!mergeItemStack(itemStack, invEnd, shortEnd, false) && !mergeItemStack(itemStack, invStart, invEnd, false)) {
                return ItemStack.EMPTY;
            }
        }

        if (itemStack.isEmpty()) {
            slot.putStack(ItemStack.EMPTY);
        } else {
            slot.onSlotChanged();
        }

        if (itemStack.getCount() == stack.getCount()) {
            return ItemStack.EMPTY;
        }

        slot.onTake(playerIn, itemStack);

        return stack;
    }

    //当物品的位置切换时关闭GUI
    //防止刷物品
    @Override
    public boolean canInteractWith(PlayerEntity playerIn) {
        ItemStack hand = playerIn.getHeldItem(Hand.MAIN_HAND);
        ItemStack off = playerIn.getHeldItem(Hand.OFF_HAND);
        return !hand.isEmpty() && hand == shield || !off.isEmpty() && off == shield;
    }
//
//    @OnlyIn(Dist.CLIENT)
//    public float getEnergyScaled() {
//        ItemIntegratedShield item = (ItemIntegratedShield) shield.getItem();
//        IEnergyStorage energyCap = item.getEnergyCap(shield);
//        int i = energyCap.getMaxEnergyStored();
//        int j = energyCap.getEnergyStored();
//        return i == 0 || j == 0 ? 0 : (float) j / i;
//    }
//
//    @OnlyIn(Dist.CLIENT)
//    public int getEnergy() {
//        ItemIntegratedShield item = (ItemIntegratedShield) shield.getItem();
//        IEnergyStorage energyCap = item.getEnergyCap(shield);
//        return energyCap.getEnergyStored();
//    }
}
